const express = require('express');
const app = express();
//把socket.io挂载到express上
//获取express底层的http服务器

const http = require('http').createServer(app);

//加载socket.io模块时，把路由挂载到底层的http服务之上
const serversocket = require('socket.io')(http, { cors: true })
const cors = require('cors');
//设置public目录为静态资源托管目录
app.use(express.static('public'))
app.use(cors())
serversocket.on('connection', (socket) => {

    console.log('有客户端进来了：' + socket.id);

    socket.on('massage', function(res) {
        console.log(res);
        serversocket.emit('massage', res)
    });
    socket.on('answer', (res) => {
        serversocket.emit('answer1', res)
    })

});

http.listen(8081, () => {
    console.log('server is running.......0')
})